#ifndef	__BFX__REF_NAME_TABLE_H__
#define	__BFX__REF_NAME_TABLE_H__

namespace BFX
{

// Table of atomized string objects. This provides an efficient means for the XML parser to use the
// same string object for all repeated element and attribute names in an XML document. 
class REFNameTable : public REFObject
{
public:
	virtual ~REFNameTable();

	// Gets the atomized String object containing the same chars as the specified range of chars in the given char array.
	virtual UniString Get(UCSTR pch, int iOffset, int nLength) = 0;
	// Gets the atomized String object containing the same value as the specified string.
	virtual UniString Get(const UniString& str) = 0;
	// Creates a new atom for the characters at the specified range of chararacters in the specified string.
	virtual UniString Add(UCSTR pch, int iOffset, int nLength) = 0;
	// Creates a new atom for the specified string.
	virtual UniString Add(const UniString& str) = 0;
};

// xml-nametable implemented as a simple hash table.
class REFHashNameTable : public REFNameTable
{
private:
	struct Entry : public REFObject
	{
		UniString	m_str;
		int			m_nHashCode;
		REF<Entry>	m_pNext;
		Entry(const UniString& str, int nHashCode, Entry* pNext)
		{
			m_str = str;
			m_nHashCode = nHashCode;
			m_pNext = pNext;
		}
	};

public:
	REFHashNameTable();
	virtual ~REFHashNameTable();

	virtual UniString Get(UCSTR pchName, int iOffset, int nLength);
	virtual UniString Get(const UniString& strName);
	virtual UniString Add(UCSTR pchName, int iOffset, int nLength);
	virtual UniString Add(const UniString& strName);
	void Clear();

	static int GetHashCode(const UniString& strName);

private:
	static const int HASH_MASK	= 0x1f;	// 32 - 1

	REF<Entry>*	m_pBuckets;
	int	m_nSize;
};

}	//	namespace BFX

#endif	//	__BFX__REF_NAME_TABLE_H__
